package com.zhaopin.config;

import com.zhaopin.interceptors.TimeInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {
    /**
     * 添加视图控制器
     * 立即访问
     * <mvc:view-controller path="/" view-name="index" />
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/tuling").setViewName("hello");
    }



    /**
     * 添加拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new TimeInterceptor())     //添加拦截器
            .addPathPatterns("/**")    // 拦截映射规则
            .excludePathPatterns("/pages/**");  // 设置排除的映射规则
    }

    /**
     * 全局CORS配置
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/user/*")   // 映射服务器中那些http接口运行跨域访问
        .allowedOrigins("http://localhost:8081")     // 配置哪些来源有权限跨域
        .allowedMethods("GET","POST","DELETE","PUT");   // 配置运行跨域访问的请求方法
    }

}
